package cn.iocoder.yudao.module.member.alliance.service;

import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.business.eligibility.service.AdminEligibilityBaseService;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.member.alliance.dal.dataobject.CloudAllianceUserDO;
import cn.iocoder.yudao.module.member.alliance.dal.mysql.CloudAllianceUserMapper;
import cn.iocoder.yudao.module.member.alliance.vo.AdminCloudAllianceUserQueryDTO;
import cn.iocoder.yudao.module.member.alliance.vo.AdminCloudAllianceUserVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Map;

/**
 * 云盟商信息 Service 实现类
 *
 * @author 山野羡民
 */
@Service
@Validated
public class AdminCloudAllianceUserServiceImpl extends AdminEligibilityBaseService<CloudAllianceUserDO, AdminCloudAllianceUserVO, AdminCloudAllianceUserQueryDTO> implements AdminCloudAllianceUserService {
    @Resource
    private CloudAllianceUserMapper cloudAllianceUserMapper;

    @Resource
    private MemberUserApi memberUserApi;

    @Override
    protected CloudAllianceUserMapper getInjectedMapper() {
        return cloudAllianceUserMapper;
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<CloudAllianceUserDO> buildQueryWrapper(AdminCloudAllianceUserQueryDTO param) {
        CoreQueryWrapper<CloudAllianceUserDO> wrapper = super.buildQueryWrapper(param);
        wrapper.gt("purchase_count", 0);
        wrapper.eqIfPresent("type", param.getType());
        if (param.getValid() != null) {
            if (param.getValid()) {
                wrapper.gt("expire_time", LocalDateTime.now());
            } else {
                wrapper.le("expire_time", LocalDateTime.now());
            }
        }
        return wrapper;
    }

    @Nonnull
    @Override
    protected Map<Long, UserSimpleVO> getUserSimpleMap(Collection<Long> userIds) {
        return memberUserApi.getUserSimpleMap(userIds);
    }

}